package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.base.extmodel.inner.OrgCheckTreeNode;
import com.augurit.common.base.extmodel.inner.OrgTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmOrg;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;

public class OmOrgConverter {

	public static void convertToEntity(OmOrgForm form, OmOrg entity){
		if(form != null && entity != null){
			
			if(form.getOrgId() != null)
				entity.setOrgId(form.getOrgId());
			
			if(form.getOrgCode() != null && form.getOrgCode().trim().length() > 0)
				entity.setOrgCode(form.getOrgCode());
			
			if(form.getOrgName() != null && form.getOrgName().trim().length() > 0)
				entity.setOrgName(form.getOrgName());
			
			if(form.getOrgType() != null && form.getOrgType().trim().length() > 0)
				entity.setOrgType(form.getOrgType());
			
			if(form.getOrgLevel() != null)
				entity.setOrgLevel(form.getOrgLevel());
			
			if(form.getOrgGrade() != null && form.getOrgGrade().trim().length() > 0)
				entity.setOrgGrade(form.getOrgGrade());
			
			if(form.getParentOrgId() != null)
				entity.setParentOrgId(form.getParentOrgId());
			
			if(form.getOrgSeq() != null && form.getOrgSeq().trim().length() > 0)
				entity.setOrgSeq(form.getOrgSeq());
			
			if(form.getOrgAddr() != null && form.getOrgAddr().trim().length() > 0)
				entity.setOrgAddr(form.getOrgAddr());
			
			if(form.getZipCode() != null && form.getZipCode().trim().length() > 0)
				entity.setZipCode(form.getZipCode());
			
			if(form.getDirectorPosId() != null)
				entity.setDirectorPosId(form.getDirectorPosId());
			
			if(form.getDirectorUserId() != null)
				entity.setDirectorUserId(form.getDirectorUserId());
			
			if(form.getLinkMan() != null && form.getLinkMan().trim().length() > 0)
				entity.setLinkMan(form.getLinkMan());
			
			if(form.getLinkTel() != null && form.getLinkTel().trim().length() > 0)
				entity.setLinkTel(form.getLinkTel());
			
			if(form.getEmail() != null && form.getEmail().trim().length() > 0)
				entity.setEmail(form.getEmail());
			
			if(form.getWebUrl() != null && form.getWebUrl().trim().length() > 0)
				entity.setWebUrl(form.getWebUrl());
			
			if(form.getStatus() != null && form.getStatus().trim().length() > 0)
				entity.setStatus(form.getStatus());
			
			if(form.getArea() != null && form.getArea().trim().length() > 0)
				entity.setArea(form.getArea());
			
			if(form.getCreator() != null && form.getCreator().trim().length() > 0)
				entity.setCreator(form.getCreator());
			
			if(form.getCreateTime() != null)
				entity.setCreateTime(form.getCreateTime());
			
			if(form.getUpdator() != null && form.getUpdator().trim().length() > 0)
				entity.setUpdator(form.getUpdator());
			
			if(form.getUpdateTime() != null)
				entity.setUpdateTime(form.getUpdateTime());
			
			if(form.getSortNo() != null)
				entity.setSortNo(form.getSortNo());
			
			if(form.getIsLeaf() != null && form.getIsLeaf().trim().length() > 0)
				entity.setIsLeaf(form.getIsLeaf());
			else
				entity.setIsLeaf(OrgConstant.ZERO);
			
			if(form.getSubCount() != null)
				entity.setSubCount(form.getSubCount());
			else
				entity.setSubCount(0);
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static List<OmOrgForm> convertToForm(List<OmOrg> list){
		List<OmOrgForm> result = null;
		
		if(list != null && list.size() > 0){
			result = new ArrayList();
			
			for(OmOrg entity : list){
				result.add(convertToForm(entity));
			}
		}
		
		return result;
	}
	
	public static OmOrgForm convertToForm(OmOrg entity){
		if(entity != null){
			OmOrgForm form = new OmOrgForm();

			form.setOrgId(entity.getOrgId());
			form.setOrgCode(entity.getOrgCode());
			form.setOrgName(entity.getOrgName());
			form.setOrgType(entity.getOrgType());
			form.setOrgLevel(entity.getOrgLevel());
			form.setOrgGrade(entity.getOrgGrade());
			form.setParentOrgId(entity.getParentOrgId());
			form.setOrgSeq(entity.getOrgSeq());
			form.setOrgAddr(entity.getOrgAddr());
			form.setZipCode(entity.getZipCode());
			form.setDirectorPosId(entity.getDirectorPosId());
			form.setDirectorUserId(entity.getDirectorUserId());
			form.setLinkMan(entity.getLinkMan());
			form.setLinkTel(entity.getLinkTel());
			form.setEmail(entity.getEmail());
			form.setWebUrl(entity.getWebUrl());
			form.setStatus(entity.getStatus());
			form.setArea(entity.getArea());
			form.setCreator(entity.getCreator());
			form.setCreateTime(entity.getCreateTime());
			form.setUpdator(entity.getUpdator());
			form.setUpdateTime(entity.getUpdateTime());
			form.setSortNo(entity.getSortNo());
			form.setIsLeaf(entity.getIsLeaf());
			form.setSubCount(entity.getSubCount());
			form.setRemark(entity.getRemark());
			return form;
		}else
			return null;
	}
	
	/**
	 * 将机构Form对象列表转换为ExtTreeNode对象列表
	 * @param result
	 * @return
	 */
	public static List<OrgTreeNode> convertToTreeNode(List<OmOrgForm> list){
		List<OrgTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(OmOrgForm org : list)
				result.add(convertToOrgTreeNode(org));
		}
		return result;
	}
	
	/**
	 * 将机构Form对象转换为OrgTreeNode对象
	 * @param form
	 * @return
	 */
	public static OrgTreeNode convertToOrgTreeNode(OmOrgForm form){
		if(form != null){
			OrgTreeNode node = new OrgTreeNode();
			
			node.setText(form.getOrgName());
			node.setQtip(form.getOrgCode());
			node.setIconCls(OrgTreeNode.CLS_ORG);
			node.setLeaf(false);
			
			node.setPartyType(OrgConstant.PARTY_TYPE_ORG);
			node.setOrgId(form.getOrgId());
			
			if(form.isChecked())
				node.setCanManager(true);
			else
				node.setCanManager(false);
			
			return node;
		}else
			return null;
	}
	
	public static List<OmOrgForm> convertToFormList(List<OmOrg> list){
		return convertToFormList(list, null,null,null);
	}
	
	public static List<OmOrgForm> convertToFormList(List<OmOrg> list, 
			List<SysCodeForm> orgTypeList,
			List<SysCodeForm> orgGradeList, 
			List<SysCodeForm> orgStatusList){
		
		if(list != null && list.size() > 0){
			List<OmOrgForm> result = new ArrayList();
			
			for(OmOrg entity : list){
				OmOrgForm form = convertToForm(entity);
				
				//扩展属性转换
				form.setOrgTypeLabel(OrgUtils.getItemName(form.getOrgType(), orgTypeList));
				form.setOrgGradeLabel(OrgUtils.getItemName(form.getOrgGrade(), orgGradeList));
				form.setOrgStatusLabel(OrgUtils.getItemName(form.getStatus(), orgStatusList));
				
				result.add(form);
			}
			
			return result;
		}else
			return null;
	}
	
	//将岗位List<OmPositionForm>转化为List<BasicCombo>
	public static List<BasicCombo> convertPosListToBasicCombo(List<OmPositionForm> list, boolean allowBlank){
		List<BasicCombo> result = new ArrayList();
		
		if(allowBlank)
			result.add(BasicCombo.getBlankItem());
		
		if(list != null && list.size() > 0){
			for(OmPositionForm form : list){
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getPosName());
				combo.setValue(form.getPosId().toString());
				result.add(combo);
			}
		}
		
		return result;
	}
	
	//将人员List<OmUserForm>转化为List<BasicCombo>
	public static List<BasicCombo> convertUserListToBasicCombo(List<OmUserForm> list, boolean allowBlank){
		List<BasicCombo> result = new ArrayList();
		
		if(allowBlank)
			result.add(BasicCombo.getBlankItem());
		
		if(list != null && list.size() > 0){
			for(OmUserForm form : list){
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getUserName());
				combo.setValue(form.getUserId().toString());
				result.add(combo);
			}
		}
		
		return result;
	}
	
	
	/**
	 * 将机构OmOrgForm列表转化为机构权限树
	 * @param list 需要显示的节点
	 * @param authedOrgIds 用户已经授权的节点，也是需要打勾的节点
	 * @param orgIds ，管理用户拥有的机构权限，也是需要带checkBox显示的节点，
	 * @return 机构权限树
	 * */
	public static List convertToExtNode(List<OmOrgForm> list){
		List result = null;
		
		if(list != null && list.size()>0 ){
			result =  new ArrayList();
			
			for(OmOrgForm form : list){
				if(form.isChecked())
					result.add(convertToOrgCheckTreeNode(form));
				else
					result.add(convertToExtNode(form));
			}
		}
		return result;
	}
	
	public static OrgTreeNode convertToExtNode(OmOrgForm form){
		OrgTreeNode node =null;
		if(form != null){
			 node = new OrgTreeNode();
			 node.setText(form.getOrgName());
			 node.setQtip(form.getOrgCode());
			 node.setIconCls(OrgTreeNode.CLS_ORG);
			 node.setLeaf(false);	
			 
			 node.setOrgId(form.getOrgId());
		}
		return node;
	}
	
	public static OrgCheckTreeNode convertToOrgCheckTreeNode(OmOrgForm form){
		OrgCheckTreeNode node = null;
		if(form != null){
			node = new OrgCheckTreeNode();
			
			node.setText(form.getOrgName());
			node.setQtip(form.getOrgCode());
			node.setIconCls(OrgTreeNode.CLS_ORG);
			node.setLeaf(false);
			
			node.setOrgId(form.getOrgId());
		}
		return node;
	}
	
	//转换成OrgTreeNode,还要设置isCanManager属性
	public static List<OrgTreeNode> convertToTreeNodeListByPriv(/*boolean isSystemAdmin,List<Long> privChildrenOrgIds,*/OmOrgForm form){
		List<OrgTreeNode> result = new ArrayList();
		
		if(form != null){
			
			//转换子机构列表
			if(form.getChildren() != null && form.getChildren().size() > 0){
				for(OmOrgForm org : form.getChildren())
					result.add(convertToOrgTreeNode(/*isSystemAdmin,privChildrenOrgIds,*/org));
			}
			
			//转换岗位列表
			if(form.getPostions() != null && form.getPostions().size() > 0){
				for(OmPositionForm position : form.getPostions())
					result.add(OmPositionConverter.convertToOrgTreeNode(position));
			}
			
			//转换用户列表
			if(form.getUsers() != null && form.getUsers().size() > 0){
				for(OmUserForm user : form.getUsers())
					result.add(OmUserConverter.convertToOrgTreeNode(user, form.getOrgId(), null));
			}
		}
		
		return result;
	}
}
